package com.info33.platform.common.util;

import com.info33.platform.system.util.enums.UserTypeEnum;
import com.info33.platform.system.vo.SysTenantUserVO;
import com.info33.platform.system.vo.SysUserVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.util.ObjectUtils;

import java.util.Optional;

/**
 * @author debj
 * @version 1.0
 * @date 2021/7/26 14:54
 */
@SuppressWarnings("unused")
public class SysUtil {

    private SysUtil() {
    }

    /**
     * 检测是否为开发环境
     *
     * @param environment-
     * @return -
     */
    public static Boolean isDev(Environment environment) {
        return environment.acceptsProfiles(Profiles.of("dev"));
    }

    /**
     * 获取当前用户
     *
     * @return -
     */
    public static SysUserVO getCurrentUser() {
        Subject subject = SecurityUtils.getSubject();
        return (SysUserVO) subject.getPrincipal();
    }

    /**
     * 当前用户是否是当前租户下的管理员
     *
     * @return -
     */
    public static Boolean isSysAdmin() {
        return isSysAdmin(getCurrentUser());
    }

    /**
     * 当前用户是否是当前租户下的管理员
     *
     * @param sysUserVO 当前用户对象
     * @return -
     */
    public static Boolean isSysAdmin(SysUserVO sysUserVO) {
        if (ObjectUtils.isEmpty(sysUserVO)) {
            return Boolean.FALSE;
        }
        return sysUserVO.getSysTenantUserVO().getUserType().equals(UserTypeEnum.ADMIN);
    }

    /**
     * 当前用户是否是 Admin 租户下的 租户管理员
     *
     * @return -
     */
    public static Boolean isSysTenantAdmin() {
        return isSysTenantAdmin(getCurrentUser());
    }

    /**
     * 当前用户是否是 Admin 租户下的 租户管理员
     *
     * @param sysUserVO 当前用户对象
     * @return -
     */
    public static Boolean isSysTenantAdmin(SysUserVO sysUserVO) {
        if (ObjectUtils.isEmpty(sysUserVO)) {
            return Boolean.FALSE;
        }
        return isSysAdmin(sysUserVO) &&
                sysUserVO.getTenant().getType().equals(UserTypeEnum.ADMIN);
    }

    /**
     * 当前用户若是非管理员则返回用户 id
     *
     * @return 用户 id
     */
    public static Long getNoAdminId() {
        SysUserVO currentUser = getCurrentUser();
        return Boolean.TRUE.equals(isSysAdmin(currentUser)) ?
                null :
                Optional
                        .ofNullable(currentUser)
                        .map(SysUserVO::getSysTenantUserVO)
                        .map(SysTenantUserVO::getId)
                        .orElse(null);
    }

    /**
     * 当前用户若是非管理员则返回用户 id
     *
     * @return 用户 id
     */
    public static Long getTenantId(SysUserVO currentUser) {
        return Optional
                .ofNullable(currentUser)
                .map(SysUserVO::getSysTenantUserVO)
                .map(SysTenantUserVO::getId)
                .orElse(null);
    }

    /**
     * 当前用户若是非管理员则返回用户 id
     *
     * @return 用户 id
     */
    public static Long getTenantId() {
        return getTenantId(getCurrentUser());
    }

}
